package com.example.demo54acl.controller;

import com.example.demo54acl.domain.NoticeMessage;
import com.example.demo54acl.service.NoticeMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class NoticeMessageController {

    @Autowired
    private NoticeMessageService noticeMessageService;

    @GetMapping("/findAll")
    public List<NoticeMessage> findAll(){
        List<NoticeMessage> all = noticeMessageService.findAll();
        System.out.println(all);
        return all;
    }
    @GetMapping("/findById/{id}")
    public NoticeMessage findById(@PathVariable Integer id){
        NoticeMessage message = noticeMessageService.findById(id);
        System.out.println(message);
        return message;
    }
    @GetMapping("/update/{id}")
    public NoticeMessage update(@PathVariable Integer id){
        NoticeMessage message = noticeMessageService.findById(id);
        if (message != null){
            message.setContent("update " + message.getContent());
            noticeMessageService.update(message);
            message = noticeMessageService.findById(id);
        }
        return message;
    }
    @GetMapping("/save/{content}/{id}")
    public String save(@PathVariable String content,@PathVariable Integer id){
        NoticeMessage message = new NoticeMessage();
        message.setId(id);
        message.setContent(content);
        noticeMessageService.save(message);
        return "save success";
    }

}
